//
//  HOTCommWebViewController.m
//  Genpara
//
//  Created by zhang chunhui on 14-9-30.
//  Copyright (c) 2014年 zhangchh. All rights reserved.
//

#import "HOTCommWebViewController.h"

@interface HOTCommWebViewController ()

@property(nonatomic,copy)NSString *urlStr;
@end

@implementation HOTCommWebViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil url:(NSString *)urlStr
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.urlStr = urlStr;
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:self.urlStr]];
    
    [_webView loadRequest:request];
}

- (IBAction)previousBtnClicked:(id)sender {
    
    [_webView goBack];
}

- (IBAction)forwordBtnClicked:(id)sender {
    
    [_webView goForward];
}

- (IBAction)stopBtnClicked:(id)sender {
    
    [_webView stopLoading];
}

- (IBAction)reloadBtnClicked:(id)sender {
    
    [_webView reload];
}

-(void)showToolBar:(BOOL)isShow {
    
    NSLayoutConstraint *bottomCons = nil;
    for (NSLayoutConstraint *constraint in self.view.constraints) {
        
        if (constraint.firstItem == _webView
            && constraint.secondItem == _toolBar
            && constraint.firstAttribute == NSLayoutAttributeBottom
            && constraint.secondAttribute == NSLayoutAttributeTop) {
            
            bottomCons = constraint;
            break;
        }
    }
    if (isShow) {
        
        _toolBar.hidden = NO;
        if (bottomCons.secondItem == self.view) {
            
            [self.view removeConstraint:bottomCons];
            
            [self.view addConstraint:[NSLayoutConstraint constraintWithItem:_webView attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:_toolBar attribute:NSLayoutAttributeTop multiplier:1.0 constant:0]];
        }

    } else {
        
        _toolBar.hidden = YES;
        if (bottomCons.secondItem == _toolBar) {
            [self.view removeConstraint:bottomCons];
            
            [self.view addConstraint:[NSLayoutConstraint constraintWithItem:_webView attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeBottom multiplier:1.0 constant:0]];
        }
    }
}

-(void)backBtnClicked:(id)sender {
    
    if (self.presentingViewController) {
        
        [self dismissViewControllerAnimated:YES completion:nil];
    } else {
        
        [self.navigationController popViewControllerAnimated:YES];
    }
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
